#include <Servo.h>
Servo myservo;  // 定义Servo对象来控制
int pos = 0;    // 角度存储变量
int pina=8;//继电器
int pin=7;//土壤湿度检测,湿度大为0
void setup() {
    Serial.begin(57600);
   myservo1.attach(3);  //手爪电机
   myservo2.attach(5);  //上臂电机
   myservo3.attach(6); //下臂电机
   myservo4.attach(9); //底座电机
  //手爪 Servo
  MIN[0] = 10;
  MAX[0] = 50;
 INITANGLE[0] = 30;
  //上臂电机
  MIN[1] = 10; // This should bring the lever to just below 90deg to ground
  MAX[1] = 140;
  INITANGLE[1] = 90; // This should bring the lever parallel with the ground
  //下臂电机
  MIN[2] = 40;
  MAX[2] = 170;
  INITANGLE[2] = 90;
  //底座电机
  MIN[3] = 0;
  MAX[3] = 170;
  INITANGLE[3] = 90;
   pinMode(pin,INPUT);
pinMode(pina,OUTPUT);
digitalWrite(pina) = 0;
}

void loop() {
  if(digitalRead(pin) == 1){
     move(pos);
digitalWrite(pina) = 1;
  }
}

void move(int x,int y) {
  //控制舵机角度x变为y
  if(x<y){
    for (pos = x; pos <= y; pos ++) { // 0°到180°
    // in steps of 1 degree
    myservo.write(pos);              // 舵机角度写入
    delay(5);                       // 等待转动到指定角度
  }
  else if(x>y){
    for (pos = x; pos >= y; pos --) { // 0°到180°
    // in steps of 1 degree
    myservo.write(pos);              // 舵机角度写入
    delay(5);                       // 等待转动到指定角度
  }


}
